package gogogo.liwushuo.fragment;


import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import gogogo.liwushuo.R;
import gogogo.liwushuo.bean.ScollerBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.util.OKHttpUtils;

public class HomeFragment extends Fragment {


    @InjectView(R.id.toolbar_home)
    Toolbar toolbarHome;
    @InjectView(R.id.scollerview_rg_tab)
    RadioGroup scollerviewRgTab;
    @InjectView(R.id.scollerview_iv)
    ImageView scollerviewIv;
    @InjectView(R.id.viewpager)
    ViewPager viewpager;

    private static final String TAG = "mtag";
    private OKHttpUtils okHttpUtils;
    private Context mContext;
    private String jsonDataScollerView;
    private ScollerBean scollerBean;
    private List<ScollerBean.DataBean.ChannelsBean> channels;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.inject(this, view);

        return view;
    }



    private void initData() {
        mContext = getActivity();
        Log.i(TAG, "initData: "+(mContext == null));
        okHttpUtils = OKHttpUtils.newInstance(mContext);
        okHttpUtils.getAsyncMainData(Resource.URL_HORIZTSCOLLVIEW, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                Log.i(TAG, "onFailure: ");
            }

            @Override
            public void onResponse(String result) {
                jsonDataScollerView = result;
                Log.i(TAG, "onResponse: "+result);
                scollerBean = new Gson().fromJson(jsonDataScollerView,ScollerBean.class);
                channels = scollerBean.getData().getChannels();
                initRadioGroup();
            }
        });
    }

    private void initRadioGroup() {
        for (int i = 0; i < channels.size(); i++) {
            RadioButton mRb = new RadioButton(mContext);
            RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            mRb.setLayoutParams(lp);
            mRb.setButtonDrawable(null);
            String name = channels.get(i).getName();
            mRb.setText(name);
            mRb.setPadding(22,22,22,22);
//            mRb.setTextColor(Resource.COLOR_RB_UNCHECKED);
//            if (mRb.isChecked()){
//                mRb.setTextColor(Resource.COLOR_RB_CHECKED);
//            }
            mRb.setTextColor(getResources().getColorStateList(R.color.selector_textcolor));
            scollerviewRgTab.addView(mRb);
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        toolbarHome.setTitle("");
        initData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
